package com.ms.pda.activity

import android.view.View
import androidx.databinding.ObservableBoolean
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.*
import com.ms.architecture.base.DataBindingConfig
import com.ms.pda.BR
import com.ms.pda.R
import com.ms.pda.base.BaseActivity
import com.ms.pda.messenger.AppMessenger
import com.ms.pda.messenger.LoginMessenger
import com.ms.pda.model.Constants

open class LoginActivity : BaseActivity() {

    private var mState: LoginViewModel? = null
    private var mMsger: LoginMessenger? = null
    private var mAppMsger: AppMessenger? = null

    override fun initViewModel() {
        mState = getActivityScopeViewModel(LoginViewModel::class.java)
        mMsger = getActivityScopeViewModel(LoginMessenger::class.java)
        mAppMsger = getApplicationScopeViewModel(AppMessenger::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_login, BR.state, mState!!)
            .addBindingParam(BR.msger, mMsger!!)
            ?.addBindingParam(BR.listener, LoginListener())
    }

    override fun initViews() {
        checkUserCache()
    }

    /**
     * 是否可以点击登录
     */
    private fun checkIsCanLogin() {
        val isPass = !StringUtils.isTrimEmpty(mMsger!!.account.value) &&
                !StringUtils.isTrimEmpty(mMsger!!.password.value)
        mState!!.canLogin.set(isPass)
    }

    /**
     * 检查登录信息缓存
     */
    private fun checkUserCache() {
        val loginCache = SPUtils.getInstance().getString(Constants.LOGIN_INFO, "")
        val remember = SPUtils.getInstance().getBoolean(Constants.PWD_REMEMBER, true)
        mState!!.remember.set(remember)

        //如果不存在缓存
        if (StringUtils.isTrimEmpty(loginCache)) {
            val account = SPUtils.getInstance().getString(Constants.ACCOUNT, "")
            val pwd = SPUtils.getInstance().getString(Constants.PASSWORD, "")
            mMsger!!.account.observe(this) { checkIsCanLogin() }
            mMsger!!.password.observe(this) { checkIsCanLogin() }
            mMsger!!.account.value = account
            mMsger!!.password.value = pwd
            return
        }
        ActivityUtils.startActivity(MainActivity::class.java)
        ActivityUtils.finishActivity(this)
    }


    /**
     * 点击事件
     */
    open inner class LoginListener {
        //记住密码
        open fun rememberPassword(view: View) {
            val remember: Boolean = mState!!.remember.get() ?: true
            mState!!.remember.set(!remember)
        }

        //登录按钮
        open fun login(view: View) {
            mMsger!!.login(this@LoginActivity) {
                SPUtils.getInstance().put(Constants.ACCOUNT, mMsger!!.account.value!!)
                mState!!.remember.get().let {
                    SPUtils.getInstance().put(Constants.PASSWORD, mMsger!!.password.value!!)
                }
                SPUtils.getInstance().put(Constants.LOGIN_INFO, "admin")
                ActivityUtils.startActivity(MainActivity::class.java)
                ActivityUtils.finishActivity(this@LoginActivity)
            }
        }

    }

    class LoginViewModel : ViewModel() {
        val canLogin: ObservableBoolean = ObservableBoolean(false)
        val remember: ObservableBoolean = ObservableBoolean(true)
    }
}